#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    this->worker = new Worker();
    this->t = new QThread(this);
    connect(this->t, &QThread::started, worker, &Worker::init);
    this->t->start();

    connect(worker, &Worker::ready_list, [=](){
        ui->fromBox->addItems(worker->languageList);
    });
    connect(worker, &Worker::ready_list, [=](){
        ui->toBox->addItems(worker->languageList);
    });
    connect(this, &MainWindow::request, worker, &Worker::request);
    connect(worker, &Worker::send_response, [=](char *response){
        ui->resualt->setText(response);
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_toBox_currentTextChanged(const QString &arg1)
{
    this->m_to = arg1;
}


void MainWindow::on_fromBox_currentTextChanged(const QString &arg1)
{
    this->m_from = arg1;
}


void MainWindow::on_translation_clicked()
{
    // 待翻译文本
    char *q;
    q = ui->inp->toPlainText().toUtf8().data();
    emit this->request(q, this->m_from.toUtf8().data(), this->m_to.toUtf8().data());
}

